$(function(){
	initBlockUI();
   $("#search_block input").click(nofunc);
   $("#search_block select").change(nofunc);
   updateTable(0, "#pager");
   initPagination();
});

// 分页
function initPagination()
{
	$("#pager").pagination($("#wholepage").val(), {
		items_per_page: 10,
		prev_text: "前一页",
		next_text: "后一页",
		callback: updateTable
	});
}

// 更新表格
function updateTable(page, pager_block)
{
   var url = "address_list.php";
   var pars = { "page_status": 1, "page": (page + 1) };
   $.post(url, pars, function(xmlobj){
	  $("#tbody2").html("");
	  $(xmlobj).find("item").each(function(i){
		 var archid = $(this).find("archiveid").text();
		 var username = $(this).find("username").text();
		 var sex = getSex($(this).find("sex").text());
		 var tel = $(this).find("tel").text();
		 var phone = $(this).find("phone").text();
		 $("<tr></tr>")
		   .append($("<th></th>").html((i + 1)))
		   .append($("<td></td>").html((username + sex)))
		   .append($("<td></td>").html(tel))
		   .append($("<td></td>").html(phone))
		   .append($("<td></td>")
				   .html($("<a href=\"#\"></a>")
						 .attr("archid", archid)
						 .html("详细信息")
						 .click(function(){
							showInfo($(this).attr("archid"));
							return false;
						 })
						 )
				   )
		 .appendTo("#tbody2");
	  });
	  tablePretty("#tbody2");
   });
   return false;
}

// 获取性别信息
function getSex(sex) { return (sex == "1") ? " 先生" : " 女士"; }

// 打开详细信息
function showInfo(id)
{
   var url = "address_list.php";
   var pars = { "page_status": 2, "id": id };
   $.post(url, pars, function(xmlobj){
	  var username	 = $(xmlobj).find("username").text();
	  var sex		 = $(xmlobj).find("sex").text();
	  var tel		 = $(xmlobj).find("tel").text();
	  var phone		 = $(xmlobj).find("phone").text();
	  var qq		 = $(xmlobj).find("qq").text();
	  var email		 = $(xmlobj).find("email").text();
	  var address	 = $(xmlobj).find("address").text();
	  var photo		 = $(xmlobj).find("photo").text();
	  
	  tel 		= (tel != "") ? tel : "无";
	  phone		= (phone != "") ? phone : "无";
	  qq		= (qq != "") ? qq : "无";
	  email		= (email != "") ? ($("<a href=\"mailto:" + email + "\"></a>").html(email)) : "无";
	  address	= (address != "") ? address : "无";
	  
	  $("#preview_block")
		.find("#preview_photo").attr("src", photo).end()
		.find("#preview_name").html(username + getSex(sex)).end()
		.find("#preview_tel").html(tel).end()
		.find("#preview_phone").html(phone).end()
		.find("#preview_qq").html(qq).end()
		.find("#preview_email").html(email).end()
		.find("#preview_address").html(address).end()
		.modal({
			 containerCss: {
				  "height": "250px",
				  "width": "700px",
				  "margin-left": "-350px",
				  "top": "20%"
			 },
		   onShow: function() { m(1); },
		   onClose: function() { m(0); }
		});
   });
}


function nofunc()
{
   window.alert("啊哦，该功能还未实现哦！");
}

// modal 的隐现
function m(a) { modalShowClose("#search_block input, #search_block select", a); }
